package reflex_and_annotation.b_get_attribute;

import reflex_and_annotation.entity.User;

import java.lang.reflect.Field;

/**
 * @Author WLJ
 * @Description //TODO 获取属性
 * @Date 14:38 2023/8/29
 * @Param 
 * @return 
 **/
public class Test {
    public static void main(String[] args) {
        method3();
    }
    
    private static void method1() {
        Class clazz = getClazz();
        // 获取所有的public修饰的实现
        Field[] fields = clazz.getFields();
        for (Field field: fields) {
            System.out.println(field.getName() +
                    "<==>" + field.getType() +
                    "<==>" + field.getGenericType() +
                    "<==>" + field.getModifiers());
        }
    }

    private static void method2() {
        Class clazz = getClazz();
        // 获取所有属性（慎用）
        Field[] fields = clazz.getDeclaredFields();
        for (Field field: fields) {
            System.out.println(field.getName() +
                    "<==>" + field.getType() +
                    "<==>" + field.getGenericType() +
                    "<==>" + field.getModifiers());
        }
    }
    /**
     * @Author WLJ
     * @Description //TODO 通过属性名获取属性并赋值
     * @Date 15:04 2023/8/29
     * @Param []
     * @return void
     **/
    private static void method3() {
        Class clazz = getClazz();
        User user = new User();
        try {
            Field i = clazz.getDeclaredField("i");
            System.out.println(i.getName());
            // 非公共属性无法通过对象赋值
//            i.setInt(user, 1);
//            System.out.println(user.getI());
            System.out.println("============");
            // 获取public修饰的属性
            Field pubs = clazz.getField("pubs");
            pubs.set(user, "123");
            System.out.println(user.pubs);
            System.out.println("============");
            // 获取public修饰的静态属性
            Field pubss = clazz.getField("pubss");
            pubss.set(user, "pubss");
            System.out.println(pubss.get(user));
            // 静态属性本身就属于这个类，所以不用传对象
            Field pubss1 = clazz.getField("pubss");
            pubss1.set(null, "pubss1");
            System.out.println(pubss1.get(null));
            System.out.println("==============");
            // 获取private修饰的静态属性（报错）
//            Field priss = clazz.getDeclaredField("priss");
//            priss.set(null, "priss");
//            System.out.println(priss.get(null));
            // 获取父类的属性
            Field fatherI = clazz.getField("fatherI");
            System.out.println(fatherI.getName());
            fatherI.set(user, 22);
            System.out.println(user.fatherI);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author WLJ
     * @Description //TODO 获取class对象
     * @Date 14:49 2023/8/29
     * @Param []
     * @return java.lang.Class
     **/
    private static Class getClazz() {
        try {
            return Class.forName("reflex_and_annotation.entity.User");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
